<template>
  <!-- 增改用户弹框 -->
  <hcDialog
    dialogWidth="30%"
    :dialogTitle="options.type === 'add' ? '新增类型' : '编辑类型'"
    :isVisable="options.show"
    @closeDialog="closeFun"
    @openDialog="openFun"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="6vw"
      status-icon
      :rules="rules"
      :validate-on-rule-change="false"
    >
      <el-form-item prop="recipe_type" label="配方类型">
        <el-select v-model="form.recipe_type">
          <el-option
            v-for="item of recipeArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="attribute_type" label="属性类型">
        <el-select v-model="form.attribute_type">
          <el-option
            v-for="item of attributeArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="station_id" label="工站编号">
        <el-select v-model="form.station_id">
          <el-option
            v-for="item of stationArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="enabled" label="是否可用">
        <el-select v-model="form.enabled">
          <el-option label="可用" :value="true"></el-option>
          <el-option label="禁用" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="btns">
          <el-button type="primary" @click="saveDialog()">{{
            t("common.saveText")
          }}</el-button>
          <el-button @click="closeFun">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </hcDialog>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import useDictionary from "@/hooks/useDictionary";
import { AddRecipeType, updateRecipeType } from "@/api/recipe";
import { getStation } from "@/api/process";
import { useI18n } from "vue-i18n";
let { t } = useI18n();

const emit = defineEmits(["closeDialog"]);

const props = defineProps({
  options: {
    type: Object,
    default: () => {
      return {
        show: false,
        type: "add", // 用于判断是编辑还是删除 add edit
        data: null, // 当前编辑的用户
      };
    },
  },
});

const formRef = ref();
const form = reactive({
  id: 0,
  recipe_type: "",
  attribute_type: "",
  station_id: null,
  enabled: true,
});
let stationArr = ref([]);
let attributeArr = ref([]);
let recipeArr = ref([]);

const rules = {
  attribute_type: [
    { required: true, message: "请输入属性类型", trigger: "blur" },
  ],
  recipe_type: [{ required: true, message: "请输入配方类型", trigger: "blur" }],
};

function saveDialog() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let res = "";
      if (props.options.type === "edit") {
        res = await updateRecipeType(form);
      } else {
        res = await AddRecipeType(form);
      }
      if (res) {
        ElMessage.success("保存成功");
        emit("closeDialog", true);
      } else {
        ElMessage.error("保存失败");
      }
    }
  });
}

// 关闭弹框
const closeFun = () => {
  formRef.value?.resetFields();
  emit("closeDialog");
};

function openFun() {
  if (props.options.type === "edit") {
    for (let key in form) {
      form[key] = props.options.data[key];
    }
  }
}

async function getAllStation() {
  let res = await getStation();
  stationArr.value = res?.items?.map((item) => {
    return {
      label: item.station_code,
      value: item.id,
    };
  });
}

function getDic() {
  useDictionary({ dictType: "RECIPE_TYPE" }).then(
    (res) => (recipeArr.value = res)
  );
  useDictionary({ dictType: "ATTRIBUTE_TYPE" }).then(
    (res) => (attributeArr.value = res)
  );
}

onMounted(() => {
  getAllStation();
  getDic();
});
</script>
<style lang="scss" scoped></style>
